*&---------------------------------------------------------------------*
*& Report ZEX_LISTING_83                                               *
*&---------------------------------------------------------------------*
*& Created By: James Wood (james.wood@bowdarkconsulting.com)           *
*& Created On: 12/12/2008                                              *
*& Purpose:    This program shows how an illegal cast exception can be *
*&             dealt with using the TRY statement. It also shows how   *
*&             to extract the details of an exception using an         *
*&             exception object's methods.                             *
*&---------------------------------------------------------------------*
REPORT zex_listing_83.

*----------------------------------------------------------------------*
*       CLASS lcl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_parent DEFINITION.

  PUBLIC SECTION.
    METHODS: a, b.

ENDCLASS.                    "lcl_parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_parent IMPLEMENTATION.

  METHOD a.
    WRITE: / 'In method a.'.
  ENDMETHOD.                    "a

  METHOD b.
    WRITE: / 'In method b.'.
  ENDMETHOD.                    "b

ENDCLASS.                    "lcl_parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS lcl_child  DEFINITIO
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_child DEFINITION
      INHERITING FROM lcl_parent.

  PUBLIC SECTION.
    METHODS: c.

ENDCLASS.                    "lcl_child  DEFINITIO

*----------------------------------------------------------------------*
*       CLASS lcl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_child IMPLEMENTATION.

  METHOD c.
    WRITE: / 'In method c.'.
  ENDMETHOD.                    "c

ENDCLASS.                    "lcl_child IMPLEMENTATION

*----------------------------------------------------------------------*
* START-OF-SELECTION Event Module                                      *
*----------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM test_exceptions.

*&---------------------------------------------------------------------*
*&      Form  test_exceptions
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM test_exceptions.

* Local Data Declarations:
  DATA: lr_parent   TYPE REF TO lcl_parent,
        lr_child    TYPE REF TO lcl_child,
        lr_ex       TYPE REF TO cx_sy_move_cast_error,
        lv_progname TYPE syrepid,
        lv_inclname TYPE syrepid,
        lv_line     TYPE i,
        lv_text     TYPE string,
        lv_longtext TYPE string.

* Attempt a widening cast where the dynamic type of the
* source object reference is not compatible with
* the static type of the target object reference:
  TRY.
    CREATE OBJECT lr_parent.
    CREATE OBJECT lr_child.
    MOVE lr_parent ?TO lr_child.
  CATCH cx_sy_move_cast_error INTO lr_ex.
*   Read information about the exception:
    CALL METHOD lr_ex->get_source_position
      IMPORTING
        program_name = lv_progname
        include_name = lv_inclname
        source_line  = lv_line.

    lv_text = lr_ex->get_text( ).
    lv_longtext = lr_ex->get_longtext( ).
    CONDENSE lv_longtext.

*   Output an error report about the exception:
    WRITE: / 'Error Report'.
    ULINE.
    WRITE: / 'Program Name:', lv_progname.
    WRITE: / 'Include Name:', lv_inclname.
    WRITE: / 'Line Number:', lv_line.
    WRITE: / 'Short Text:', lv_text.
    WRITE: / 'Long Text:', lv_longtext.
  ENDTRY.

ENDFORM.                    "test_exceptions